package com.hl.toolkit.admin;

import com.tycoin.common.enums.MediaStatusEnum;
import com.tycoin.common.enums.OnlineStatusEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class AdminGlobal {

	public static String siteCode;

	public static boolean injectionEnabled = false;

	public static String getSiteCodePath() {
		return siteCode;
	}

	public static String getSiteCode() {
		if ("center".equalsIgnoreCase(siteCode)) {
			return "";
		}
		return siteCode;
	}

	@Value("${site.code}")
	public void setSiteCode(String siteCode) {
		AdminGlobal.siteCode = siteCode;
	}

	public static boolean isInjectionEnabled() {
		return injectionEnabled;
	}

	@Value("${injection.enabled:false}")
	public void setInjectionEnabled(boolean injectionEnabled) {
		AdminGlobal.injectionEnabled = injectionEnabled;
	}

	public static String imageUploadPath;

	public static String imageWebPath;

	public static String xmlUploadPath;

	public static String xmlWebPath;

	public static String getImageUploadPath() {
		return imageUploadPath;
	}

	@Value("${image.upload.path:}")
	public void setImageUploadPath(String imageUploadPath) {
		AdminGlobal.imageUploadPath = imageUploadPath;
	}

	public static String getImageWebPath() {
		return imageWebPath;
	}

	@Value("${image.web.path:}")
	public void setImageWebPath(String imageWebPath) {
		AdminGlobal.imageWebPath = imageWebPath;
	}

	public static String getStatusMothodName(int status) {
		OnlineStatusEnum statusEnum = OnlineStatusEnum.getEnumByKey(status);
		switch (statusEnum) {
		case ONLINE:
			return "下线";
		default:
			return "上线";
		}
	}

	public static String getImagePath(String path) {
		if (StringUtils.isEmpty(path)) {
			return "";
		}
		if (path.indexOf("http") == 0) {
			return path;
		}
		return getImageWebPath() + StringUtils.trimToEmpty(path);
	}

	public static String getXmlUploadPath() {
		return xmlUploadPath;
	}

	@Value("${xml.upload.path:}")
	public void setXmlUploadPath(String xmlUploadPath) {
		AdminGlobal.xmlUploadPath = xmlUploadPath;
	}

	public static String getXmlWebPath() {
		return xmlWebPath;
	}

	@Value("${xml.web.path:}")
	public void setXmlWebPath(String xmlWebPath) {
		AdminGlobal.xmlWebPath = xmlWebPath;
	}

	public static String getXmlPath(String path) {
		if (StringUtils.isEmpty(path)) {
			return "";
		}
		if (path.indexOf("http") == 0) {
			return path;
		}
		return getXmlWebPath() + StringUtils.trimToEmpty(path);
	}

	public static String getMediaStatusDesc(int mediaStatus) {
		StringBuffer sb = new StringBuffer();
		if (mediaStatus == MediaStatusEnum.OK.getKey()) {
			sb.append("<span class=\"badge badge-success\">")
					.append(MediaStatusEnum.OK.getValue()).append("</span>");
			return sb.toString();
		}

		if (isMediaStatusOpen(mediaStatus, MediaStatusEnum.DOWNLOAD.getKey())) {
			sb.append("<span class=\"badge badge-info\">")
					.append(MediaStatusEnum.DOWNLOAD.getValue())
					.append("</span>");
		}

		if (isMediaStatusOpen(mediaStatus,
				MediaStatusEnum.DOWNLOAD_FAIL.getKey())) {
			sb.append("<span class=\"badge badge-danger\">")
					.append(MediaStatusEnum.DOWNLOAD_FAIL.getValue())
					.append("</span>");
		}

		if (isMediaStatusOpen(mediaStatus,
				MediaStatusEnum.DOWNLOAD_PART_FAIL.getKey())) {
			sb.append("<span class=\"badge badge-danger\">")
					.append(MediaStatusEnum.DOWNLOAD_PART_FAIL.getValue())
					.append("</span>");
		}

		if (isMediaStatusOpen(mediaStatus, MediaStatusEnum.MISS_VIDEO.getKey())) {
			sb.append("<span class=\"badge badge-danger\">")
					.append(MediaStatusEnum.MISS_VIDEO.getValue())
					.append("</span>");
		}

		if (isMediaStatusOpen(mediaStatus, MediaStatusEnum.MISS_IMAGE.getKey())) {
			sb.append("<span class=\"badge badge-danger\">")
					.append(MediaStatusEnum.MISS_IMAGE.getValue())
					.append("</span>");
		}

		return sb.toString();
	}

	public static boolean isMediaStatusOpen(int mediaStatus, int mask) {
		if (mask <= 0) {
			return false;
		}
		int index = (mediaStatus / mask);
		if (index % 10 == 1) {
			return true;
		}
		return false;
	}

	public static int setMediaStatusOpen(int mediaStatus, int... masks) {
		int result = mediaStatus;
		for (int mask : masks) {
			if (!isMediaStatusOpen(result, mask)) {
				result += mask;
			}
		}
		return result;
	}

	public static int setMediaStatusClose(int mediaStatus, int... masks) {
		int result = mediaStatus;
		for (int mask : masks) {
			if (isMediaStatusOpen(result, mask)) {
				result -= mask;
			}
		}
		return result;
	}

	public static boolean isStatusOpen(int status, int mask) {
		if (mask <= 0) {
			return false;
		}
		int index = (status / mask);
		if (index % 10 == 1) {
			return true;
		}
		return false;
	}

	public static int setStatusOpen(int status, int... masks) {
		int result = status;
		for (int mask : masks) {
			if (!isStatusOpen(result, mask)) {
				result += mask;
			}
		}
		return result;
	}

	public static int setStatusClose(int status, int... masks) {
		int result = status;
		for (int mask : masks) {
			if (isStatusOpen(result, mask)) {
				result -= mask;
			}
		}
		return result;
	}

}
